﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace BestClient
{
    public class BestData
    {
        //Standard BEST Data Format
        /*
        {
            "sid":"xx1.xx2.xx3.xx4",
            "mid": "xxxxxxxx", 
            "sys":"command",
            "platform":"android",
            "lang":"EN",
            "sn" :"xxxxxxxx",
            "payload":{
                "params": {
                    "key1":"value1",
                    "key2":"value2"
                    }
                }
        }
         * */
        public string sid { get; set; }
        public string mid { get; set; }
        public string sys { get; set; }
        public string platform { get; set; }
        public string lang { get; set; }
        public string sn { get; set; }
        public string usr { get; set; }
        public string comp { get; set; }
        public string sign { get; set; }
        public string token { get; set; }
        public string data { get; set; }
        public string code { get; set; }
        public string message { get; set; }        
        public Dictionary<string, object> payload{get; set;}

        private const string PARAMS_KEY = "params";
        private const string RESULTS_KEY = "results";
        private const string PARAMS_LIST_KEY = "paramList";

        public object getPayload(string key)
        {
            return payload[key];
        }

        public void setPayload(string key, object objValue)
        {
            if (payload == null)
            {
                payload = new Dictionary<string, object>();
            }

            if (payload.Keys.Contains(key))
            {
                payload[key] = objValue;
            }
            else
            {
                payload.Add(key, objValue);
            }
        }

        public object getPayloadParam()
        {
            return getPayload(PARAMS_KEY);
        }

        public object getPayloadParam(string key)
        {
            //Dictionary<string, object> para = (Dictionary<string, object>) payload[PARAMS_KEY];
            //return para[key];
            JObject para = (JObject)payload[PARAMS_KEY];
            return (JObject)para[key].ToString();
        }

        public object getPayloadParamList()
        {
            return getPayload(PARAMS_LIST_KEY);
        }

        private object getPayloadResult(int index)
        {
            if(payload.ContainsKey(RESULTS_KEY)){

                JArray ja = (JArray)payload[RESULTS_KEY];

                if(ja.Count > 0 && index <= ja.Count){

                    return ja[index];
                }
                else{
                    return null;
                }
            }
            return null;
            
            //payload[RESULTS_KEY]
            //List<Dictionary<string, object>> results = (List<Dictionary<string, object>>)payload[RESULTS_KEY];
            //if (results == null || index >= results.Count())
            //{
            //    return null;
            //}            
            //return results[index];
        }

        public object getPayloadResultKey(string key)
        {
            JObject map = (JObject)getPayloadResult(0);

            //Dictionary<string, JObject> resultMap = (Dictionary<string, JObject>)map;
            object obj = null;        
            
            if (map[key] is JArray)
            {
                //obj = (object)(((JArray)map[key]).ToList());
                JArray jarray = (JArray)map[key];
                return (object)(jarray.ToList());
            }
            else
            {
                obj = (object)map[key].ToString(); 
            }
            return obj;
        }

        public void setPayloadParam(string key, object value) {
            if (payload == null) {
                payload = new Dictionary<string, object>();
            }

            Dictionary<string, object> Params = null;
            if (payload.ContainsKey(PARAMS_KEY))
            {
                Params = (Dictionary<string, object>)payload[PARAMS_KEY];
            }
            if (Params == null) {
                Params = new Dictionary<String, Object>();
            }

            Params.Add(key, value);

            if (payload.ContainsKey(PARAMS_KEY))
            {
                payload[PARAMS_KEY] = Params;
            }
            else { 
                payload.Add(PARAMS_KEY, Params);
            }


            //if (payload == null) {
            //    payload = new HashMap<String, Object>();
            //}
            //Map<String, Object> params = (Map<String, Object>) payload.get(PARAMS_KEY);
            //if (params == null) {
            //    params = new HashMap<String, Object>();
            //}
            //params.put(key, value);
            //payload.put(PARAMS_KEY, params);
        }

        public void setPayloadParamList(List<Object> paramList)
        {
            if (payload == null)
            {
                payload = new Dictionary<string, object>();
            }

            if (payload.ContainsKey(PARAMS_LIST_KEY)) { 
                payload.Add(PARAMS_LIST_KEY, paramList);
            }
            else
            {
                payload[PARAMS_LIST_KEY] = paramList;
            }
        }

        public void setPayloadResult(List<object> map)
        {
            if (payload == null)
            {
                payload = new Dictionary<string, object>();
            }

            List<object> results = null;
            if (payload.ContainsKey(RESULTS_KEY))
            {
                results = (List<object>)payload[RESULTS_KEY];
            }
            if (results == null)
            {
                results = new List<object>();
            }
            results.AddRange(map);

            if (payload.ContainsKey(RESULTS_KEY))
            {
                payload[RESULTS_KEY] = results;
            }
            else
            {
                payload.Add(RESULTS_KEY, results);
            }
        }
        
        public void setPayloadResult(List<Dictionary<string, object>> map)
        {
            if (payload == null)
            {
                payload = new Dictionary<string, object>();
            }

            List<Dictionary<string, object>> results = null;
            if(payload.ContainsKey(RESULTS_KEY)){
                results = (List<Dictionary<string, object>>)payload[RESULTS_KEY];
            }
            if (results == null)
            {
                results = new List<Dictionary<string, object>>();
            }
            results.AddRange(map);

            if (payload.ContainsKey(RESULTS_KEY))
            {
                payload[RESULTS_KEY] =  results;
            }
            else
            {
                payload.Add(RESULTS_KEY, results);
            }
        }

        public override string ToString()
        {
            return "BestData{" +
                    "sid='" + sid + '\'' +
                    ", sys='" + sys + '\'' +
                    ", usr='" + usr + '\'' +
                    ", sn='" + sn + '\'' +
                    ", comp='" + comp + '\'' +
                    ", sign='" + sign + '\'' +
                    ", token='" + token + '\'' +
                    ", data='" + data + '\'' +
                    ", code='" + code + '\'' +
                    ", message='" + message + '\'' +
                    ", mid='" + mid + '\'' +
                    ", lang='" + lang + '\'' +
                    ", platform='" + platform + '\'' +
                    ", payload=" + payload +
                    '}';
        }

    }
}
